package com.atguigu.juc.base;

import ch.qos.logback.core.util.TimeUtil;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author yanglin
 * @create 2023-12-26 14:11 星期二
 * description:
 */
public class DaemonDemo {
    
    @Test
    public void testDaemon(){
        Thread t1 = new Thread(() -> {
            System.out.println(Thread.currentThread().getName() + "\t 开始运行 \t" + 
                    (Thread.currentThread().isDaemon() ? "守护线程" : "用户线程"));
        },"t1");
        
        //将t1设置为守护线程  需要在start方法设置之前设置
        t1.setDaemon(true);
        t1.start();

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + "\t ---end 主线程");
    }
    
}
